﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
public class BulletBase : MonoBehaviour
{
    public string LauchEffectName = "Launch_";
    public string TrailEffectName = "Trail_";

    [HideInInspector]
    public Bullet bulletBase
    {
        get
        {
            return GameMgr.Get<IBulletManager>().GetBullet(gameObject.name);
        }
    }

    public virtual void Awake()
    {
    }
    public virtual void OnEnable()
    {
        Alive = true;
        GetComponent<SpriteRenderer>().enabled = false;
        StartCoroutine(LauchEffect());
    }

    GameObject clone;
    GameObject clone2;
    //发射特效
    IEnumerator LauchEffect()
    {
        yield return 0;
        EffectFunc();
    }
    public void EffectFunc()
    {
        if (GameMgr.Get<IBulletManager>().GetAllBullet()._BulletList.ContainsKey(gameObject.name))
        {
            GetComponent<SpriteRenderer>().enabled = true;
            GetComponent<SpriteRenderer>().color = GetAttrColor();
            if (LauchEffectName != "")
            {
                clone = ResourceManager.Instance.Instantiate("Prefabs/Effects/" + LauchEffectName + GameMgr.Get<IMonoManager>().CurrentAttrName(bulletBase.attribute));
                clone.transform.position = transform.position;
            }
            if (TrailEffectName != "")
            {
                clone2 = ResourceManager.Instance.Instantiate("Prefabs/Effects/" + TrailEffectName + GameMgr.Get<IMonoManager>().CurrentAttrName(bulletBase.attribute));
                clone2.transform.position = transform.position;
                clone2.transform.parent = transform;
                var emmit = clone2.GetComponent<ParticleSystem>().emission;
                emmit.enabled = true;
            }
        }
    }
    public void ReCreateEffect()
    {
        if (clone != null)
        {
            Destroy(clone);
        }
        if (clone2 != null)
        {
            Destroy(clone2);
        }
        EffectFunc();
    }
    public Color GetAttrColor()
    {
        if(bulletBase == null)
        {
            return new Color(1, 1, 1, 1);
        }
        switch (bulletBase.attribute)
        {
            case MonoAttribute.None:
                return new Color(1, 1, 1, 1);
            case MonoAttribute.Fire:
                return Data.Instance.PrintColor[0];
            case MonoAttribute.Ice:
                return Data.Instance.PrintColor[1];
            case MonoAttribute.Shine:
                return Data.Instance.PrintColor[2];
            case MonoAttribute.Dark:
                return Data.Instance.PrintColor[3];
            case MonoAttribute.Steam:
                return Data.Instance.PrintColor[4];
            case MonoAttribute.Electric:
                return Data.Instance.PrintColor[5];
            case MonoAttribute.Oil:
                return Data.Instance.PrintColor[6];
            default:
                return new Color(1, 1, 1, 1);
        }
    }

    public virtual void Update()
    {
        if (GameMgr.Get<IBulletManager>().GetAllBullet()._BulletList.ContainsKey(gameObject.name))
        {
            StopCoroutine("LauchEffect");
        }
    }
    public void LateUpdate()
    {
        if (OutOfRange())
        {
            OnOutofRange();
        }
    }
    public void MoveBullet()
    {
        if (bulletBase == null)
        {
            return;
        }
        GameMgr.Get<IBulletManager>().MoveBullet(bulletBase);
    }

    private bool Alive;
    public void OnCollide(GameObject other)
    {
        if (!Alive)
        {
            return;
        }
        if (bulletBase == null)
        {
            return;
        }
        bulletBase.collision.NowNum += 1;
        if (other != null)
        {
            OnHitOthers(other);
        }
        if (bulletBase.collision.NowNum< bulletBase.collision.MaxNum)
        {
            OnBounced();
        }
        else
        {
            Alive = false;
            OnDestroyed();
        }
    }

    public virtual void OnHitOthers(GameObject other)
    {
        HitAddBuff(other);
        HitOthersEffect(transform.position);
    }
    public void HitOthersEffect(Vector3 HitPoint)
    {
        GameObject clone = ResourceManager.Instance.Instantiate("Prefabs/Effects/Explode_"+ GameMgr.Get<IMonoManager>().CurrentAttrName(bulletBase.attribute));
        clone.transform.position = HitPoint;
    }
    //属性buff伤害
    public void HitAddBuff(GameObject other)
    {
        if (other.GetComponent<MonoBase>() == null)
        {
            return;
        }
        //HitNoneMono();
        int buffID = 0;
        switch (bulletBase.attribute)
        {
            case MonoAttribute.None:
                buffID = 0;
                break;
            case MonoAttribute.Fire:
                buffID = 1;
                break;
            case MonoAttribute.Ice:
                buffID = 2;
                break;
            case MonoAttribute.Shine:
                buffID = 3;
                break;
            case MonoAttribute.Dark:
                buffID = 4;
                break;
            case MonoAttribute.Steam:
                buffID = 5;
                break;
            case MonoAttribute.Electric:
                buffID = 6;
                break;
            case MonoAttribute.Oil:
                buffID = 7;
                break;
            default:
                break;
        }
        Mono mono = GameMgr.Get<IMonoManager>().GetMono(other.name);
        //子弹本身带伤害时，在无属性时不添加空buff而直接添加伤害
        if (bulletBase.attribute == MonoAttribute.None && bulletBase.Damage != 0)
        {
            GameMgr.Get<IMonoManager>().ChangeHPValue(mono, -bulletBase.Damage);
        }
        else
        {
            GameMgr.Get<IBuffManager>().AddBuff(mono, buffID, bulletBase.AddBuffNum);
        }
    }
    public virtual void OnBounced()
    {
        bulletBase.positionData.StartPosition = bulletBase.positionData.Position;
        bulletBase.Direction = SkillManager.RotateRound(bulletBase.Direction, Vector3.forward, Random.Range(160, 200));
    }

    public virtual void OnOutofRange()
    {
        HitNoneMono();
        OnDestroyed();
    }

    public virtual void OnDestroyed()
    {
        OnDestroyEffect();
    }
    private void OnDestroyEffect()
    {
        foreach (Transform child in transform)
        {
            if (child.gameObject.name.Contains("Trail"))
            {
                child.SetParent(null);
                var emmit = child.GetComponent<ParticleSystem>().emission;
                emmit.enabled = false;
                child.GetComponent<Recycler>().LateDestroy(1);
            }
        }
    }

    public void ChangeDirection(Vector3 Direct)
    {
        if (bulletBase == null)
        {
            return;
        }
        bulletBase.Direction = Direct;
    }
    /// <summary>
    /// 射程判断
    /// </summary>
    public bool OutOfRange()
    {
        if (bulletBase == null)
        {
            return false;
        }
        return (bulletBase.positionData.Position - bulletBase.positionData.StartPosition).magnitude > bulletBase.Range;
    }
    /// <summary>
    /// 超出射程
    /// </summary>
    public virtual void HitNoneMono()
    {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (bulletBase == null)
        {
            return;
        }

        if (collision.GetComponent<MonoBase>() == null)
        {
            if (collision.tag == "SceneMono" && !collision.name.Contains("Ground"))
            {
                //HitNoneMono();
                HitOthersEffect(transform.position);
                OnDestroyed();
            }
            return;
        }
        if (GameMgr.Get<IMonoManager>().GetMono(collision.gameObject.name).monoRealtimeStatus.IsCounterAttack)
        {
            if (GameMgr.Get<IMonoManager>().GetMono(collision.gameObject.name).Attr != MonoAttribute.None)
            {
                bulletBase.attribute = GameMgr.Get<IMonoManager>().GetMono(collision.gameObject.name).Attr;
            }
            ReCreateEffect();
            OnBounced();
            return;
        }
        OnCollide(collision.gameObject);
    }
}
